package com.ruoyi.fire.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.ResponseEntityInfo;
import com.ruoyi.common.enums.RequestMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.RequestService;
import com.ruoyi.fire.domain.ERegion;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fire.mapper.ESuperviseLevelMapper;
import com.ruoyi.fire.domain.ESuperviseLevel;
import com.ruoyi.fire.service.IESuperviseLevelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 单位监管等级Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-30
 */
@Service
@RequiredArgsConstructor
public class ESuperviseLevelServiceImpl extends ServiceImpl<ESuperviseLevelMapper, ESuperviseLevel> implements IESuperviseLevelService {
    @Autowired
    private ESuperviseLevelMapper eSuperviseLevelMapper;

    private final RequestService requestService;

    /**
     * 查询单位监管等级
     *
     * @param id 单位监管等级主键
     * @return 单位监管等级
     */
    @Override
    public ESuperviseLevel selectESuperviseLevelById(Long id) {
        return eSuperviseLevelMapper.selectESuperviseLevelById(id);
    }

    /**
     * 查询单位监管等级列表
     *
     * @param eSuperviseLevel 单位监管等级
     * @return 单位监管等级
     */
    @Override
    public List<ESuperviseLevel> selectESuperviseLevelList(ESuperviseLevel eSuperviseLevel) {
        return eSuperviseLevelMapper.selectESuperviseLevelList(eSuperviseLevel);
    }

    /**
     * 新增单位监管等级
     *
     * @param eSuperviseLevel 单位监管等级
     * @return 结果
     */
    @Override
    public int insertESuperviseLevel(ESuperviseLevel eSuperviseLevel) {
        return eSuperviseLevelMapper.insertESuperviseLevel(eSuperviseLevel);
    }

    /**
     * 修改单位监管等级
     *
     * @param eSuperviseLevel 单位监管等级
     * @return 结果
     */
    @Override
    public int updateESuperviseLevel(ESuperviseLevel eSuperviseLevel) {
        eSuperviseLevel.setUpdateTime(DateUtils.getNowDate());
        return eSuperviseLevelMapper.updateESuperviseLevel(eSuperviseLevel);
    }

    /**
     * 批量删除单位监管等级
     *
     * @param ids 需要删除的单位监管等级主键
     * @return 结果
     */
    @Override
    public int deleteESuperviseLevelByIds(Long[] ids) {
        return eSuperviseLevelMapper.deleteESuperviseLevelByIds(ids);
    }

    /**
     * 删除单位监管等级信息
     *
     * @param id 单位监管等级主键
     * @return 结果
     */
    @Override
    public int deleteESuperviseLevelById(Long id) {
        return eSuperviseLevelMapper.deleteESuperviseLevelById(id);
    }

    /**
     * 数据同步
     *
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dataSync() {
        try {
            ResponseEntityInfo responseEntityInfo = requestService.advancedRequest(RequestMapper.SUPERVISE_LEVEL, null);
            if (responseEntityInfo != null && responseEntityInfo.getErrno() == 0) {
                Object data = responseEntityInfo.getData();
                if (data == null) {
                    // 记录警告日志：返回数据为空
                    return;
                }

                // 使用条件删除替代全表删除，绕过保护机制
                getBaseMapper().delete(new LambdaQueryWrapper<ESuperviseLevel>().isNotNull(ESuperviseLevel::getId));
                List<ESuperviseLevel> eSuperviseLevels = JSON.parseArray(JSON.toJSONString(data), ESuperviseLevel.class);
                if (eSuperviseLevels != null && !eSuperviseLevels.isEmpty()) {
                    // 设置更新时间
                    eSuperviseLevels.forEach(item -> item.setUpdateTime(DateUtils.getNowDate()));
                    saveBatch(eSuperviseLevels);
                }
            }
        } catch (Exception e) {
            // 记录错误日志
            throw new RuntimeException("数据同步过程中发生异常", e);
        }
    }
}
